package com.UKIT.airtickets.DB

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update

@Dao
interface UserDao {
    @Query("SELECT * FROM users")
    fun getAllUsers(): List<User>

    @Query("SELECT * FROM users WHERE id = :userId")
    fun getUserById(userId: Long): User?

    @Query("SELECT * FROM users WHERE login = :login")
    fun getUserByLogin(login: String): User?

    @Query("SELECT EXISTS(SELECT 1 FROM users WHERE login = :login)")
    fun checkUserExists(login: String): Boolean

    @Insert
    fun addUser(user: User): Long

    @Update
    fun updateUser(user: User)

    @Query("UPDATE users SET password = :newPassword WHERE id = :userId AND password = :oldPassword")
    fun updatePassword(userId: Long, oldPassword: String, newPassword: String): Int

    @Query("UPDATE users SET last_name = :lastName, first_name = :firstName WHERE id = :userId")
    fun updateUserDetails(userId: Long, lastName: String, firstName: String): Int

    @Query("UPDATE users SET avatar = :avatarUrl WHERE id = :userId")
    fun updateUserAvatar(userId: Long, avatarUrl: String): Int

    @Query("DELETE FROM users WHERE id = :userId")
    fun deleteUser(userId: Long)
}
